package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.record.environmentSterilize.EnvironmentSterilizeExcelDto;
import com.lnking.lnkingplace.model.dto.record.environmentSterilize.EnvironmentSterilizeListDto;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.record.environmentSterilize.EnvironmentSterilizeAddRequest;
import com.lnking.lnkingplace.model.request.record.environmentSterilize.EnvironmentSterilizeSearchRequest;
import com.lnking.lnkingplace.model.request.record.environmentSterilize.EnvironmentSterilizeUpdateRequest;
import com.lnking.lnkingplace.service.IRecordEnvironmentSterilizeService;
import com.lnking.starter.export.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/record-environment-sterilize")
@Api(tags = "环境消毒记录")
public class RecordEnvironmentSterilizeController {

    @Resource
    IRecordEnvironmentSterilizeService recordEnvironmentSterilizeService;
    @Resource
    UserUtil userUtil;

    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void export(EnvironmentSterilizeSearchRequest params, HttpServletResponse response) throws Exception {
        List<EnvironmentSterilizeListDto> data = (List<EnvironmentSterilizeListDto>)recordEnvironmentSterilizeService.pageList(params);
        ExportUtils.export(data, response, EnvironmentSterilizeExcelDto.class);
    }

    @ApiOperation("导入")
    @PostMapping("import")
    private ReturnResult importExcel(@RequestParam("file") MultipartFile file) throws Exception{
        recordEnvironmentSterilizeService.importExcel(file);
        return ReturnResult.success();
    }

    @ApiOperation("查询")
    @GetMapping("list")
    public ReturnResult getList(EnvironmentSterilizeSearchRequest params){
        String type = userUtil.getUser().getType();
        //隔离场所
        if(type.equals(UserType.PLACE)){
            params.setPlaceName(userUtil.getUser().getName());
        }
        return ReturnResult.success(recordEnvironmentSterilizeService.pageList(params));
    }

    @ApiOperation("新增")
    @PostMapping("add")
    public ReturnResult add(@Valid @RequestBody EnvironmentSterilizeAddRequest params){
        recordEnvironmentSterilizeService.add(params);
        return ReturnResult.success();
    }

    @ApiOperation("修改")
    @PostMapping("update")
    public ReturnResult update(@Valid @RequestBody EnvironmentSterilizeUpdateRequest params){
        recordEnvironmentSterilizeService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id){
        return ReturnResult.success(recordEnvironmentSterilizeService.getById(id));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        recordEnvironmentSterilizeService.removeById(id);
        return ReturnResult.success();
    }
}
